<?php
	// progress POST method
	if( isset($_POST['keywords']))
	{
		$keywords = $_POST['keywords'];
		
		if( empty($keywords) )
		{
			$status['error'] = __('Keywords is empty! Please fill it before submit.', 'wsna');
		}
		else
		{
			$categorie = (isset($_POST['categorie']) && !empty($_POST['categorie']))?$_POST['categorie']:__('Uncategorie', 'wsna');
			WSNA_add_alert($categorie, $keywords);
		}
	}

	if( isset($_GET['action']) && ($_GET['action'] == 'deleteAlert'))
	{
		WSNA_delete_alert($_GET['alert_id']);
	}

	$info = new WSNA_Info;
	$user = WSNA_user_info();
	$result = WSNA_list_alerts();
	$alert_count = WSNA_alert_count();
?>
<div class="wrap">
	<form id="list-alert-form" method="get" action="">
		<h2><?php echo $info->name; ?></h2>
		<?php if ( !empty($status['error']) ): ?>
			<div class="error">
				<p><?php echo $status['error']; ?></p>
			</div>
		<?php elseif ( !empty($status['update']) ): ?>
			<div class="updated fade" style="background-color: rgb(255, 251, 204);">
				<p><?php echo $status['update']; ?></p>
			</div>
		<?php endif ?>
		
		<p id="post-search">
			Welcome, <?php echo $user['display'];?></span> of <?php echo $user['role']; ?>!<br />
			<b>Status</b>: <?php echo $alert_count['used']; ?>/<?php echo $alert_count['all']; ?><br />
			<small>If your change your email address, click <a href="profile.php">here</a> to update.</small>
		</p>

		<div>
			<table class="widefat">
				<thead>
					<tr>
						<th class="check-column" scope="col"><input type="checkbox" onclick="CheckAll(document.getElementById('posts-filter'));" /></th>
						<th scope="col"><?php echo __('Categorie', 'wsna') ?></th>
						<th scope="col"><?php echo __('Keywords', 'wsna') ?></th>
						<th class="action-links" scope="col"><?php echo __('Options', 'wsna') ?></th>
					</tr>
				</thead>
				<tbody class="list:cat" id="the-list">
					<?php
						if ( $result )
						{
							foreach ( $result as $item )
							{
								echo '<tr id="alert-id-'.$item->alert_id.'">
									<th class="check-column" scope="row"><input type="checkbox" value="4" name="delete[]"/></th>
									<td class="col">'.$item->alert_categorie.'</td>
									<td class="col">'.$item->alert_keywords.'</td>
									<td class="action-links" scope="col">
										<a href="#">Edit</a> | 
										<a class="delete" href="admin.php?page=ws-news-alerts/alerts-manager.php&action=deleteAlert&alert_id='.$item->alert_id.'">'.__('Delete', 'wsna').'</a></td>
									</tr>';
							}
						}
					?>
				</tbody>
			</table>
			<?php if ($result): ?>
			<div class="tablenav options">
				<a href="#" class="button"><?php echo __('Delete Keyword(s)', 'wsna') ?></a>
				<br class="clear"/>
			</div>
			<?php endif ?>
		</div>
	<br class="clear"/>
	</form>
	<script type="text/javascript">
	function CheckAll(form)
	{
		for (i = 0, n = form.elements.length; i < n; i++)
		{
			if(form.elements[i].type == "checkbox" && !(form.elements[i].getAttribute('onclick',2)))
			{
				if(form.elements[i].checked == true)
				{
					form.elements[i].checked = false;
				}
				else
				{
					form.elements[i].checked = true;
				}
			}
		}
	}
	</script>

	<div>
		<form id="add-alert-form" method="post" action="">
		<h2 id="add-new-alert"><?php echo __('Add New Alert', 'wsna'); ?></h2>
		<div id="ajax-response"/>
		<table class="form-table">
			<tr class="form-field">
				<th scope="row"><label for="categorie"><?php echo __('Categorie', 'wsna'); ?></label></th>
				<td>
					<input type="text" value="" id="categorie" name="categorie" /><br />
					<small><?php echo __('Categorie for kewords below. (E.g., Music)', 'wsna'); ?></small>
				</td>
			</tr>
			<tr class="form-field">
				<th scope="row"><label for="keywords"><?php echo __('Keywords', 'wsna'); ?></label></th>
				<td>
					<input type="text" value="" id="keywords" name="keywords" /><br />
					<small><?php echo __('Separate Keywords with a space. (E.g., icyleaf china beijing)', 'wsna'); ?></small>
				</td>
			</tr>
		</table>
		<p class="submit">
				<input type="submit" value="<?php echo __('Add Alert', 'wsna'); ?>" id="addalert" name="addalert"/>
		</p>
		</form>
	</div>
	
	<div class="tablenav">
		<div class="alignleft" style="padding-top:5px;">
			<?php echo $info->name.' v'.$info->version.' by <a href="'.$info->plugin.'" target="_blank">'.$info->author.'</a> | <a href="'.$info->url.'" target="_blank">'.__('What\'s this', 'wsna').'</a>?' ?>
		</div>
		<br class="clear"/>
	</div>
	<br class="clear"/>
</div>
<script type="text/javascript">
jQuery(document).ready(function(){
	jQuery('#list-alert-form a.delete').click(function(){
		if ( confirm('Do you really want to DELETE this Categorie?')==false ){
			return;
		}
	});
});
</script>
